package MovieGuessingGame.clientutil;

import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.SocketException;

import org.apache.commons.codec.binary.Base64;

public class ClientSSL {
	
	SSLSocket sslsocket = null;	
	BufferedReader bufferedreader = null;	
	PrintWriter printwriter = null;
	String message = null;

	private byte[] hashValue = null;
	
	public String getMessage() {
		return message;
	}
	
	public byte[] getHashValue() {
		return hashValue;
	}
	
	
	public void closeSocket() {
		
		if(sslsocket.isConnected()){
			try {
				sslsocket.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} 
	}
	
	public void clientConnect (String host, int port) {
		try {
			System.setProperty("javax.net.ssl.trustStore", "C:\\Users\\sgsmith\\workspace\\MovieGuessingGame\\pkeystore.jks");
			SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
			sslsocket = (SSLSocket) sslsocketfactory.createSocket(host, port);
			
			InputStream inputstream = sslsocket.getInputStream();
			InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
			bufferedreader = new BufferedReader(inputstreamreader);
			
			printwriter = new PrintWriter(sslsocket.getOutputStream(), true);
			
		} catch (Exception exception) {
			exception.printStackTrace();
		}
	}
	
	public void clientSend(String message) throws IOException{
		printwriter.println(message);
		System.out.println("clientSend msg: " + message);
	}
	
	public void sendCreds(String userName, String password) throws IOException{
		clientSend(userName + "," + password);
	}
	
	public String clientReceive() throws IOException{
		
		String response = bufferedreader.readLine();
		
		return response;
	}
	
	public int recvHash() throws IOException{
		int retVal = 0;
		
		String result = clientReceive();
		if (result.trim().equals("you are not authorized")
				|| result.equals("try again later"))
		{
			message = result;
			retVal = -1;
		}
		else 
		{
			System.out.println("DEBUG: got hash value: " + result);
			hashValue = Base64.decodeBase64(result);
			retVal = 0;
		}
		
		return retVal;
	}
	
	public void setHash(byte[] newhash){
		if(hashValue == null){
			hashValue = newhash;
			System.out.println("set hash value");
		}
	}
	
	public void sendHash() throws IOException{
		clientSend(Base64.encodeBase64String(hashValue));
	}
	
	public void setTimeout(int value) throws SocketException {
		sslsocket.setSoTimeout(value);
	}
	
	
	public static void main(String[] arstring) {
		ClientSSL client = new ClientSSL();
		try {
			client.clientConnect("localhost", 9999);
			client.sendCreds("userName", "password");
		} catch (Exception exception) {
			exception.printStackTrace();
		}
	}
}
